package com.goldcn.b2b.controller;

import com.goldcn.b2b.dto.ClassifyDiscountGroupDto;
import com.goldcn.b2b.dto.GoodsQueryDto;
import com.goldcn.b2b.service.ClassifyDiscountService;
import com.goldcn.order.dto.UserIDGoodsByDto;
import com.google.common.base.Objects;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2017/9/26.
 */
@Api(value = "采购商折扣")
@RestController
@RequestMapping(value = "/purchaser/discount")
public class ClassifyDiscountController {
    @Autowired
    ClassifyDiscountService classifyDiscountService;

    @ApiOperation(value = "新增或修改分组")
    @RequestMapping(method = RequestMethod.POST)
    public Object addClassifyDiscountGroup(@RequestBody ClassifyDiscountGroupDto discountGroupDto) {
        return classifyDiscountService.addClassifyDiscountGroup(discountGroupDto);
    }

    @ApiOperation(value = "获取分组")
    @RequestMapping(value = "/{groupID}", method = RequestMethod.GET)
    public Object get(@PathVariable Long groupID) {
        return classifyDiscountService.selectByPrimaryKey(groupID);
    }

    @ApiOperation(value = "删除分组")
    @RequestMapping(value = "/{groupID}", method = RequestMethod.DELETE)
    public Object delete(@PathVariable Long groupID) {
        return classifyDiscountService.delete(groupID);
    }

    //TODO groupID  没有传0
    @ApiOperation(value = "获取分组,type为1按分类，为2按商品")
    @RequestMapping(value = "/{type}/{groupID}", method = RequestMethod.GET)
    public Object selectClassifyDiscountGroup(@PathVariable Integer type,@PathVariable Long groupID) {
        if(Objects.equal(type,1)){
            return classifyDiscountService.selectClassifyDiscountGroup(groupID);
        }else{
            return classifyDiscountService.selectGoodsDiscountGroup(groupID);
        }
    }

    @ApiOperation(value = "获取分组列表")
    @RequestMapping(method = RequestMethod.GET)
    public Object list() {
        return classifyDiscountService.selectAll();
    }

    @ApiOperation(value = "获取分组列表分页")
    @RequestMapping(value = "/pageList/{pageNum}/{pageSize}", method = RequestMethod.GET)
    public Object page(@PathVariable Integer pageNum,@PathVariable Integer pageSize) {
        return classifyDiscountService.pageList(pageNum,pageSize);
    }


    @ApiOperation(value = "获取大类列表,userID传0查询所有")
    @RequestMapping(value = "/classfy/{userID}", method = RequestMethod.GET)
    public Object getBigClass(@PathVariable Long userID) {
        return classifyDiscountService.getSimpleBigClass(userID);
    }

    @ApiOperation(value = "获取小类列表，userID传0查询所有")
    @RequestMapping(value = "/classfy/{clazzId}/{userID}", method = RequestMethod.GET)
    public Object getSmallClass(@PathVariable String clazzId,@PathVariable Long userID) {
        return classifyDiscountService.getSimpleSmallClass(clazzId,userID);
    }

    @ApiOperation(value = "搜索商品", notes = "sort、sortOrder、pageNum、pageSize为必填字段")
    @RequestMapping(value = "/searchBar/{userID}", method = RequestMethod.POST)
    public Object searchBar(@RequestBody GoodsQueryDto queryDto,@PathVariable Long userID) {
       return classifyDiscountService.searchBar(queryDto,userID,false);
    }

    @ApiOperation(value = "搜索商品总数")
    @RequestMapping(value = "/searchBarCount/{userID}", method = RequestMethod.POST)
    public Object searchBarCount(@RequestBody GoodsQueryDto queryDto,@PathVariable Long userID) {
        return classifyDiscountService.searchBar(queryDto,userID,true);
    }

    @ApiOperation(value = "获取商品分类折扣")
    @RequestMapping(value = "/rate/{goodID}/{clazzId}/{userID}", method = RequestMethod.GET)
    public Object getDiscountRate(@PathVariable String clazzId,@PathVariable Long goodsID,@PathVariable Long userID) {
        return classifyDiscountService.getDiscountRate(clazzId,goodsID,userID);
    }

    @RequestMapping(value = "/rate/classfy/{clazzId}/{userID}", method = RequestMethod.GET)
    public Object getDiscountRateForClassfy(@PathVariable String clazzId,@PathVariable Long userID) {
        return classifyDiscountService.getDiscountRate(clazzId,0L,userID);
    }

    @RequestMapping(value = "/rate/{userID}", method = RequestMethod.POST)
    public Object getDiscountRateForClassfy2(@RequestBody UserIDGoodsByDto userIDGoodsByDto,@PathVariable Long userID) {
        return classifyDiscountService.getDiscountRate(userID,userIDGoodsByDto);
    }

    @RequestMapping(value = "/rate/goods/{goodsID}/{userID}", method = RequestMethod.GET)
    public Object getDiscountRateForGoods(@PathVariable Long goodsID,@PathVariable Long userID) {
        return classifyDiscountService.getDiscountRate("",goodsID,userID);
    }


}
